//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "ConvexMonotone.h"
using namespace Cephei::QL::Math::Interpolations;
#include <gen/QL/Math/Interpolation.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Interpolations::CConvexMonotone::CConvexMonotone (Microsoft::FSharp::Core::FSharpOption<Double>^ quadraticity, Microsoft::FSharp::Core::FSharpOption<Double>^ monotonicity, Microsoft::FSharp::Core::FSharpOption<Boolean>^ forcePositive) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phConvexMonotone = NULL;
#endif
        QuantLib::Real _quadraticity = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (quadraticity) ? (QuantLib::Real)ValueHelper::Convert (quadraticity->Value) : 0.3); //4
        QuantLib::Real _monotonicity = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (monotonicity) ? (QuantLib::Real)ValueHelper::Convert (monotonicity->Value) : 0.7); //4
        bool _forcePositive = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (forcePositive) ? (bool)ValueHelper::Convert (forcePositive->Value) : true); //4
        _ppConvexMonotone = new boost::shared_ptr<QuantLib::ConvexMonotone> (new QuantLib::ConvexMonotone ( _quadraticity,  _monotonicity,  _forcePositive ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Interpolations::CConvexMonotone::CConvexMonotone (boost::shared_ptr<QuantLib::ConvexMonotone>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phConvexMonotone = NULL;
#endif
	_ppConvexMonotone = &childNative;
    
}
Cephei::QL::Math::Interpolations::CConvexMonotone::CConvexMonotone (QuantLib::ConvexMonotone& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phConvexMonotone = NULL;
#endif
	_ppConvexMonotone = new boost::shared_ptr<QuantLib::ConvexMonotone> (&childNative);
    
    _ConvexMonotoneOwner = owner;
    
}

Cephei::QL::Math::Interpolations::CConvexMonotone::CConvexMonotone (CConvexMonotone^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phConvexMonotone = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppConvexMonotone = new boost::shared_ptr<QuantLib::ConvexMonotone> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Interpolations::CConvexMonotone::CConvexMonotone (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phConvexMonotone = NULL;
#endif
	if (!t->IsSubclassOf(CConvexMonotone::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Interpolations::CConvexMonotone::CConvexMonotone (QuantLib::Handle<QuantLib::ConvexMonotone>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phConvexMonotone = &childNative;
	_ppConvexMonotone = &static_cast<boost::shared_ptr<QuantLib::ConvexMonotone>>(childNative.currentLink());
    
    _ConvexMonotoneOwner = owner;
}
Cephei::QL::Math::Interpolations::CConvexMonotone::CConvexMonotone (QuantLib::Handle<QuantLib::ConvexMonotone> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phConvexMonotone = &childNative;
	_ppConvexMonotone = &static_cast<boost::shared_ptr<QuantLib::ConvexMonotone>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Interpolations::CConvexMonotone::CConvexMonotone (QuantLib::ConvexMonotone childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phConvexMonotone = NULL;
#endif
	_ppConvexMonotone = new boost::shared_ptr<QuantLib::ConvexMonotone> (new QuantLib::ConvexMonotone (childNative));
    
}
#endif

Cephei::QL::Math::Interpolations::CConvexMonotone::~CConvexMonotone ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppConvexMonotone != NULL)
    {
	    delete _ppConvexMonotone;
        _ppConvexMonotone = NULL;
    }
}
Cephei::QL::Math::Interpolations::CConvexMonotone::!CConvexMonotone ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppConvexMonotone != NULL)
    {
	    delete _ppConvexMonotone;
    }
}
QuantLib::ConvexMonotone& Cephei::QL::Math::Interpolations::CConvexMonotone::GetReference ()
{
    if (_ppConvexMonotone == NULL) throw gcnew NativeNullException ();
	return **_ppConvexMonotone;
}
boost::shared_ptr<QuantLib::ConvexMonotone>& Cephei::QL::Math::Interpolations::CConvexMonotone::GetShared ()
{
    if (_ppConvexMonotone == NULL) throw gcnew NativeNullException ();
	return *_ppConvexMonotone;
}
QuantLib::ConvexMonotone* Cephei::QL::Math::Interpolations::CConvexMonotone::GetPointer ()
{
    if (_ppConvexMonotone == NULL) throw gcnew NativeNullException ();
	return &**_ppConvexMonotone;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ConvexMonotone>& Cephei::QL::Math::Interpolations::CConvexMonotone::GetHandle ()
{
	if (_phConvexMonotone == NULL)
	{
		_phConvexMonotone = new Handle<QuantLib::ConvexMonotone> (*_ppConvexMonotone);
	}
	return *_phConvexMonotone;
}
#endif
bool Cephei::QL::Math::Interpolations::CConvexMonotone::HasNative () 
{
	return (_ppConvexMonotone != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Interpolations::IConvexMonotone^ Cephei::QL::Math::Interpolations::CConvexMonotone_Factory::Create (Microsoft::FSharp::Core::FSharpOption<Double>^ quadraticity, Microsoft::FSharp::Core::FSharpOption<Double>^ monotonicity, Microsoft::FSharp::Core::FSharpOption<Boolean>^ forcePositive)
{
    return gcnew CConvexMonotone ( quadraticity,  monotonicity,  forcePositive);
}
